import { isNumber } from "@/utils/is";
import { FormItemRule } from "element-plus";
import { ComponentType } from "./types";

const DATE_TYPE = ["ElDatePicker"];

function genType() {
  return [...DATE_TYPE, "DatePicker"];
}

/**
 * 时间字段
 */
export const dateItemType = genType();

export const selectType=['ElTreeSelect','ElSelectV2','ElSelect']


export function handleInputNumberValue(component?: ComponentType, val?: any) {
  if (!component) return val;
  if (['ElInput',, 'ElInputNumber'].includes(component)) {
    return val && isNumber(val) ? `${val}` : val;
  }
  return val;
}


/**
 * 设置组件规则类型
 * @param rule 规则
 * @param component 组件 
 * @param valueFormat   格式化
 */
export function setComponentRuleType(
  rule: FormItemRule,
  component: ComponentType,
  valueFormat: string,
) {
  if (dateItemType.includes(component)) {
    rule.type = valueFormat ? 'string' : 'object';
  } else if (['ElUpload', 'ElCheckboxGroup'].includes(component)) {
    rule.type = 'array';
  } else if (['ElInputNumber'].includes(component)) {
    rule.type = 'number';
  }
}